package com.aaa.statistics.service.impl;

import com.aaa.entity.CheckResult;
import com.aaa.statistics.dao.CheckItemMapper;
import com.aaa.statistics.service.CheckItemService;
import com.aaa.statistics.vo.CheckResultVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class CheckItemServiceImp implements CheckItemService {

    @Resource
    private CheckItemMapper checkItemMapper;

    @Override
    public Result<List<CheckResult>> getCheckData(CheckResultVo checkResultVo) {

        QueryWrapper<CheckResult> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(checkResultVo.getCheckItemId())){
            wrapper.eq("check_item_id",checkResultVo.getCheckItemId());
        }
        if (StringUtils.isNotBlank(checkResultVo.getPatientName())){
            wrapper.like("patient_name",checkResultVo.getPatientName());
        }
        if (Objects.nonNull(checkResultVo.getDateRange())&&checkResultVo.getDateRange().length==2){
            wrapper.between("create_time",checkResultVo.getDateRange()[0],checkResultVo.getDateRange()[1]);
        }else {
            //当天时间
            String beforeDay = LocalDate.now().minusDays(1).toString();
            String afterDay = LocalDate.now().plusDays(1).toString();
            wrapper.between("create_time",beforeDay,afterDay);
        }
        List<CheckResult> checkResults = checkItemMapper.selectList(wrapper);
        return new Result<>(200,"查询成功",checkResults);
    }

    @Override
    public Result<List<Map<String, Object>>> getCheckStatData(CheckResultVo checkResultVo) {
        Integer checkItemId = checkResultVo.getCheckItemId();
        if (Objects.nonNull(checkResultVo.getDateRange())&&checkResultVo.getDateRange().length==2){
            String beforeDate = checkResultVo.getDateRange()[0];
            String afterDate = checkResultVo.getDateRange()[1];
            List<Map<String, Object>> mapList = checkItemMapper.findCheckItemStatList(checkItemId,beforeDate,afterDate);
            return new Result<>(200,"查询成功",mapList);
        }else {
            //当天时间
            String beforeDate = LocalDate.now().minusDays(1).toString();
            String afterDate = LocalDate.now().plusDays(1).toString();
            List<Map<String, Object>> mapList = checkItemMapper.findCheckItemStatList(checkItemId,beforeDate,afterDate);
            return new Result<>(200,"查询成功",mapList);
        }
    }
}
